//
//  HomeViewController.m
//  预购清单
//
//  Created by  on 2022/10/15.
//

#import "HomeViewController.h"
#import "HomeHeader.h"
#import "HomeFooter.h"
#import "HomeCell.h"
#import "ResultViewController.h"

@interface HomeViewController ()
@property (nonatomic, assign) BOOL selectFootball;
@property (nonatomic,assign) NSInteger stamp;
@property (nonatomic, assign) CGFloat totalmoney;
@end
@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self addNotify];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    self.stamp = [[NSDate currentTimeStamp:@"yyyy-MM-dd"] integerValue];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)initUI
{
    self.selectFootball = YES;
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[HomeCell nib] forCellReuseIdentifier:[HomeCell ID]];
    self.tableView.c_top.equalTo(self.view.c_top).coffset(StuBarHight);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(0);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(0);
    self.tableView.c_bottom.equalTo(self.view.c_bottom).coffset(0);
}

- (void)loadData{
    [CCHud startHud:LS(@"数据刷新...")];
    if (APP.footRecord.count > 0) {
        [CCHud stopHud];
    }
    [self.tableView reloadData];
}
- (void)action:(NSString *)title {
    if ([title isEqualToString:LS(@"足球")]||[title isEqualToString:LS(@"篮球")]) {
        self.selectFootball = [title isEqualToString:LS(@"足球")];
        [self.tableView reloadData];
    }else if ([title isEqualToString:@"刷新"]){
        [CCHud startHud:LS(@"刷新中...") stopTime:1 handle:^{
            [self.tableView reloadData];
        }];
    }else if ([title isEqualToString:LS(@"全部")]){
        [self.tableView reloadData];
    }else if ([title isEqualToString:LS(@"赛程")] || [title isEqualToString:LS(@"赛果")]){
        ResultViewController *vc = [ResultViewController new];
        vc.title = title;
        vc.isbastball = !self.selectFootball;
        [self push:vc :YES];
    }
}
- (void)setStamp:(NSInteger )stamp{
    _stamp = stamp;
    [self loadData];
}
- (void)addNotify{
    [super addNotify];
    [NotifyTool add:self :@selector(loadData) :notifyRecordUpdate :nil];
    [NotifyTool add:self :@selector(loadData) :notifyUserUpdate :nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.selectFootball ? APP.footRecord.count : APP.basketRecord.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeCell ID]];
    cell.model = self.selectFootball ? APP.footRecord[indexPath.row] : APP.basketRecord[indexPath.row];
    return  cell;
    
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.selectFootball) {
        return APP.footRecord.count <= 0 ? 250 : 0;
    }else{
        return APP.basketRecord.count <= 0 ? 250 : 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 308;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if ((self.selectFootball && APP.footRecord.count > 0) || (!self.selectFootball && APP.basketRecord.count > 0)) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    view.backgroundColor = [UIColor clearColor];
    HomeFooter *footer = (HomeFooter *)[HomeFooter loadFromXIB];
    footer.frame = view.bounds;
    [view addSubview:footer];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 308)];
    view.backgroundColor = [UIColor clearColor];
    HomeHeader *header = (HomeHeader *)[HomeHeader loadFromXIB];
    header.frame = view.bounds;
    header.selectFootball = self.selectFootball;
    WS(self)
    header.block = ^(NSString * _Nonnull title) {
        [weakself action:title];
    };
    [view addSubview:header];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *vc = [DetailViewController new];
    vc.model = self.selectFootball ? APP.footRecord[indexPath.row] : APP.basketRecord[indexPath.row];
    [self push:vc :YES];
}

@end

